﻿using Kingdee.BOS;
using Kingdee.BOS.Core.CommonFilter;
using Kingdee.BOS.Core.Enums;
using Kingdee.BOS.Core.List;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Core.Report.PlugIn;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Resource;
using Kingdee.K3.FIN.CB.Common.BusinessEntity;
using Kingdee.K3.FIN.CB.Common.Core;
using Kingdee.K3.FIN.Core;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using static Kingdee.BOS.Core.Enums.BOSEnums;

namespace RK.ReportPlugin.Local
{
    [Description("完工入库数量查询报表实现_20240128扩展")]
    public class CompletionQtyRpt: AbstractHSRelatedRpt
    {
        protected List<string> billType = new List<string>();

        public CompletionQtyRpt()
        {
            base.HSRelatedRptEnum = (HSRelatedRpt)1;
        }

        protected override void InitCBParams()
        {
            base.InitCBParams();
            base.HaveCurrency = false;
            base.HavePeriod = true;
        }

        public override ReportHeader GetReportHeaders(IRptParams filter)
        {
            ReportHeader val = (ReportHeader)(object)new ReportHeader();
            ((ListHeader)val).AddChild("FBILLNOKEY", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("来源单据编号", "003215000024458", (SubSystemType)4, new object[0]), base.LCID));
            ((ListHeader)val).AddChild("FBILLDATEFIELD", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("业务日期", "003215000009055", (SubSystemType)4, new object[0]), base.LCID), (SqlStorageType)58, true);
            ((ListHeader)val).AddChild("FMATERIALFIELD_FNUMBER", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("产品编码", "003215000008944", (SubSystemType)4, new object[0]), base.LCID));
            ((ListHeader)val).AddChild("FMATERIALFIELD_FNAME", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("产品名称", "003215000008947", (SubSystemType)4, new object[0]), base.LCID));
            ((ListHeader)val).AddChild("FCOSTCENTERNUMBER", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("成本中心编码", "003215000008926", (SubSystemType)4, new object[0]), base.LCID));
            ((ListHeader)val).AddChild("FCOSTCENTERNAME", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("成本中心名称", "003215000008929", (SubSystemType)4, new object[0]), base.LCID));
            ((ListHeader)val).AddChild("FBASICUNITFIELD_FNAME", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("基本单位", "003215000009073", (SubSystemType)4, new object[0]), base.LCID));
            ((ListHeader)val).AddChild("FQTYFIELD", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("基本单位数量", "003215000023183", (SubSystemType)4, new object[0])), (SqlStorageType)106, true);
            ((ListHeader)val).AddChild("FPRODUCEUNIT_FNAME", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("生产单位", "003215000023184", (SubSystemType)4, new object[0])));
            ((ListHeader)val).AddChild("FPRODUCEUNITQTY", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("生产单位数量", "003215000023185", (SubSystemType)4, new object[0])), (SqlStorageType)106, true);
            ((ListHeader)val).AddChild("FSECUNITID_FNAME", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("辅库存单位", "003215000026030", (SubSystemType)4, new object[0]), base.LCID));
            ((ListHeader)val).AddChild("FSECREALQTY", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("辅库存单位数量", "003215000026031", (SubSystemType)4, new object[0]), base.LCID), (SqlStorageType)106, true);
            ((ListHeader)val).AddChild("FBATCHFIELD_FNAME", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("批号", "003215000009061", (SubSystemType)4, new object[0]), base.LCID));
            ((ListHeader)val).AddChild("FMTONO", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("计划跟踪号", "003215000010899", (SubSystemType)4, new object[0]), base.LCID));
            ((ListHeader)val).AddChild("FAIDPROPERTYFIELD_FNAME", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("辅助属性", "003215000008992", (SubSystemType)4, new object[0]), base.LCID));
            ((ListHeader)val).AddChild("FSTOCKSTATUSFIELD_FNAME", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("库存状态", "003215000009064", (SubSystemType)4, new object[0]), base.LCID));
            ((ListHeader)val).AddChild("FSTOCKFIELD_FNAME", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("仓库", "003215000009067", (SubSystemType)4, new object[0]), base.LCID));
            ((ListHeader)val).AddChild("FSTOCKLOCFIELD_FNAME", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("仓位", "003215000009070", (SubSystemType)4, new object[0]), base.LCID));
            ((ListHeader)val).AddChild("FCARGOOWNERID_FNAME", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("货主", "003215000009043", (SubSystemType)4, new object[0]), base.LCID));
            ((ListHeader)val).AddChild("FBILLTYPEID_FNAME", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("来源单据类型", "003215000024459", (SubSystemType)4, new object[0]), base.LCID));
            ((ListHeader)val).AddChild("FBILLSEQKEY", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("来源单据行号", "003215000024460", (SubSystemType)4, new object[0]), base.LCID));
            ((ListHeader)val).AddChild("FPRODUCTIONTYPE", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("生产类型", "003215000008935", (SubSystemType)4, new object[0]), base.LCID));
            ((ListHeader)val).AddChild("FORDERNUMBER", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("工单编号", "003215000008938", (SubSystemType)4, new object[0]), base.LCID));
            ((ListHeader)val).AddChild("FORDERNUMBERSEQ", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("工单行号", "003215000008941", (SubSystemType)4, new object[0]), base.LCID));
            ((ListHeader)val).AddChild("FSPECIFICATION", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("规格型号", "003215000008950", (SubSystemType)4, new object[0]), base.LCID));
            ((ListHeader)val).AddChild("FPRODUCTTYPE", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("产品类型", "003215000008986", (SubSystemType)4, new object[0]), base.LCID));
            ((ListHeader)val).AddChild("FBOMNUMBERFIELD_FNUMBER", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("BOM版本", "003215000010497", (SubSystemType)4, new object[0]), base.LCID));
            ((ListHeader)val).AddChild("FSUPPLIERFIELD_FNUMBER", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("供应商编码", "003215000023924", (SubSystemType)4, new object[0]), base.LCID));
            ((ListHeader)val).AddChild("FSUPPLIERFIELD_FNAME", (LocaleValue)(object)new LocaleValue(ResManager.LoadKDString("供应商名称", "003215000023925", (SubSystemType)4, new object[0]), base.LCID));
            GenerateDecimalControlField();
            return val;
        }

        public override void GenerateDecimalControlField()
        {
            List<DecimalControlField> list = new List<DecimalControlField>();
            DecimalControlField val = (DecimalControlField)(object)new DecimalControlField();
            val.ByDecimalControlFieldName = ("FQTYFIELD");
            val.DecimalControlFieldName = ("FQTYDIGITS");
            list.Add(val);
            DecimalControlField val2 = (DecimalControlField)(object)new DecimalControlField();
            val2.ByDecimalControlFieldName = ("FPRODUCEUNITQTY");
            val2.DecimalControlFieldName = ("FPRDQTYDIGITS");
            list.Add(val2);
            DecimalControlField val3 = (DecimalControlField)(object)new DecimalControlField();
            val3.ByDecimalControlFieldName = ("FSECREALQTY");
            val3.DecimalControlFieldName = ("FSECQTYDIGITS");
            list.Add(val3);
            ((AbstractSysReportServicePlugIn)this).ReportProperty.DecimalControlFieldList = (list);
        }

        public override Dictionary<string, KeyAlaises> GetDataFieldByCfg()
        {
            Dictionary<string, KeyAlaises> dataFieldByCfg = base.GetDataFieldByCfg();
            string empty = string.Empty;
            _ = string.Empty;
            KeyAlaises val = (KeyAlaises)(object)new KeyAlaises();
            val = (KeyAlaises)(object)new KeyAlaises();
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FBILLSEQKEY", "FBILLSEQKEY", string.Format(" {0} int not null default 0", "FBILLSEQKEY")));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dataFieldByCfg, "FBILLSEQKEY", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FBillDateField", "FBillDateField", string.Format(" {0} datetime null", "FBillDateField")));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dataFieldByCfg, "FBillDateField", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FMTONO", "FMTONO", string.Format(" {0} nvarchar(510) null", "FMTONO")));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dataFieldByCfg, "FMTONO", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            empty = GetAlias("FProduceUnit", "FNAME");
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FProduceUnit", "FProduceUnit", string.Format(" {0} int not null default 0", "FProduceUnit")));
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FNAME", empty, $" {empty} nvarchar(255) null default ''"));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dataFieldByCfg, "FProduceUnit", val);
            val = (KeyAlaises)(object)new KeyAlaises();
            ((Collection<KeyAlais>)(object)val).Add((KeyAlais)(object)new KeyAlais("FProduceUnitQty", "FProduceUnitQty", string.Format(" {0} decimal(28,10) null", "FProduceUnitQty")));
            BillExtension.AddWithoutExists<string, KeyAlaises>((IDictionary<string, KeyAlaises>)dataFieldByCfg, "FProduceUnitQty", val);
            return dataFieldByCfg;
        }

        public override Dictionary<string, string> CreateSpecialFields()
        {
            Dictionary<string, string> dictionary = base.CreateSpecialFields();
            dictionary.Add("FPRODUCTTYPE", $" FPRODUCTTYPE nvarchar(255) not null default N'{base.CBInternationalConst.MainProduct}',");
            return dictionary;
        }

        public override void TransferDataToRptTable(string strTempTable, string tableName, Dictionary<string, KeyAlaises> dataFields)
        {
            UpdateProductUnitAndUnitQty(strTempTable);
            base.TransferDataToRptTable(strTempTable, tableName, dataFields);
        }

        public override List<string> UpdateOtherReportInfos(string strTempTable)
        {
            List<string> list = base.UpdateOtherReportInfos(strTempTable);
            if (!IsUpdateSrcBillInfo)
            {
                list.AddRange(UpdateSrcBillInfoForInstock(strTempTable));
                list.AddRange(UpdateSrcBillInfoForMRB(strTempTable));
            }
            list.Add(UpdatePrdProductType(strTempTable, "PRD_RetStock"));
            list.Add(UpdatePrdProductType(strTempTable, "PRD_INSTOCK"));
            list.Add(UpdateProductType(strTempTable));
            return list;
        }

        public override string GetTransWhereSql()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendFormat(base.GetTransWhereSql());
            if (billType != null && billType.Count > 0)
            {
                stringBuilder.AppendFormat(" AND tmp.FPRODUCTIONTYPE IN (N'{0}')", string.Join("',N'", billType));
            }
            return stringBuilder.ToString();
        }

        public override List<Kingdee.BOS.Core.Report.SummaryField> GetSummaryColumnInfo(IRptParams filter)
        {
            List<Kingdee.BOS.Core.Report.SummaryField> list = new List<Kingdee.BOS.Core.Report.SummaryField>();
            list.Add((Kingdee.BOS.Core.Report.SummaryField)(object)new Kingdee.BOS.Core.Report.SummaryField("FQTYFIELD", (Enu_SummaryType)1));
            list.Add((Kingdee.BOS.Core.Report.SummaryField)(object)new Kingdee.BOS.Core.Report.SummaryField("FPRODUCEUNITQTY", (Enu_SummaryType)1));
            list.Add((Kingdee.BOS.Core.Report.SummaryField)(object)new Kingdee.BOS.Core.Report.SummaryField("FSECREALQTY", (Enu_SummaryType)1));
            return list;
        }

        public override void ConvertCustomFilterToCondition(IRptParams filter, AbstractFilterCondition condition)
        {
            base.ConvertCustomFilterToCondition(filter, condition);
            DynamicObject customFilter = filter.FilterParameter.CustomFilter;
            billType = new List<string>();
            string value = BillExtension.GetValue<string>(customFilter, "MULBILLTYPE");
            FilterRows = new List<FilterRow>();
            FilterString = string.Empty;
            using (List<string>.Enumerator enumerator = value.Split(',').ToList().GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    switch (enumerator.Current)
                    {
                        case "PO":
                            billType.Add(base.CBInternationalConst.Producted_Comm);
                            break;
                        case "OUTSRC":
                            billType.Add(base.CBInternationalConst.Producted_WW);
                            break;
                        case "SPO":
                            billType.Add(base.CBInternationalConst.Producted_Sim);
                            break;
                        case "REM":
                            billType.Add(base.CBInternationalConst.Producted_Rem);
                            break;
                    }
                }
            }
            FilterRows = filter.FilterParameter.FilterRows;
            FilterString = filter.FilterParameter.FilterString;
        }

        public override string AddSpecialReportFields()
        {
            return " ,prdunit.FPRECISION as FPRDQTYDIGITS,secunit.FPRECISION as FSECQTYDIGITS";
        }

        public override string GetSpecialJoinFields()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendFormat(" LEFT JOIN {0} prdunit ON tmp.{1}=prdunit.FUNITID", "T_BD_UNIT", "FProduceUnit");
            stringBuilder.AppendFormat(" LEFT JOIN {0} secunit ON tmp.{1}=secunit.FUNITID", "T_BD_UNIT", "FSECUNITID");
            return stringBuilder.ToString();
        }
    }
}
